package com.netty.demo.server.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

/**
 * 网络相关的工具类
 *
 * @author Administrator 余秀良
 * @version 1.0
 * <p>date: 2019年09月11日  09:44 </p>
 * <p>address: 四川成都 </p>
 */
public final class NetworkUtil {
    private NetworkUtil() {
        throw new UnsupportedOperationException("这是一个工具类不能实例化");
    }

    /**
     * 获取 内网 ip地址
     *
     * @return 内网 ip地址列表
     * @author 余秀良
     * @version 1.0
     * <p>  date: 2019 - 09 - 11  10:31    </p>
     * <p>  address: 四川成都  </p>
     */
    public static List<String> getLocalIpList() {
        try {
            //拿到所有网卡
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            List<String> ipList = new LinkedList<>();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                Enumeration<InetAddress> enumeration = networkInterface.getInetAddresses();
                while (enumeration.hasMoreElements()) {
                    InetAddress inetAddress = enumeration.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.getHostAddress().indexOf(':') == -1) {
                        String hostAddress = inetAddress.getHostAddress();
                        if (StringUtil.isNotEmpty(hostAddress)) {
                            ipList.add(hostAddress);
                        }
                    }
                }
            }
            return ipList;
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取 外网 ip地址
     *
     * @return 外网 ip地址列表
     * @author 余秀良
     * @version 1.0
     * <p>  date: 2019 - 09 - 11  10:31    </p>
     * <p>  address: 四川成都  </p>
     */
    public static List<String> getInternetIpList() {
        try {
            //拿到所有网卡
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            List<String> ipList = new LinkedList<>();
            //遍历所有网卡
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                Enumeration<InetAddress> enumeration = networkInterface.getInetAddresses();
                while (enumeration.hasMoreElements()) {
                    InetAddress inetAddress = enumeration.nextElement();
                    if (inetAddress.isLinkLocalAddress() && inetAddress.getHostAddress().indexOf(':') == -1) {
                        String hostAddress = inetAddress.getHostAddress();
                        if (StringUtil.isNotEmpty(hostAddress)) {
                            ipList.add(hostAddress);
                        }
                    }
                }
            }
            return ipList;
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }
}
